package com.ovo.chatRobot.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.iflytek.cloud.speech.ResourceUtil;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechEvent;
import com.iflytek.cloud.speech.SpeechSynthesizer;
import com.iflytek.cloud.speech.SynthesizerListener;

public class TextToVoice {
	// 语音合成对象
	private static SpeechSynthesizer mTts;
	// 合成的文本内容
	private static String mText = "语音技术实现了人机语音交互，使人与机器之间沟通变得像人与人沟通一样简单。";
	private static Map<String, String[]> mVoiceMap = new LinkedHashMap<String, String[]>();
	private static Map<String, String> mParamMap = new HashMap<String, String>();
	private static class DefaultValue {
		public static final String ENG_TYPE = SpeechConstant.TYPE_CLOUD;
		public static final String VOICE = "小燕";
		public static final String BG_SOUND = "0";
		public static final String SPEED = "50";
		public static final String PITCH = "50";
		public static final String VOLUME = "50";
		public static final String SAVE = "0";
	}

	public static void init() {
		// 初始化
//		SpeechUtility.createUtility("appid=" + Utils.getAppid());
		// 初始化合成对象
		mTts = SpeechSynthesizer.createSynthesizer();

		initParamMap();
		initMenu();
//		String mText = "今天的天气";
//		mTts.startSpeaking(mText, mSynListener);

	}
	
	public static void toSpeaking(String words) {
//		words="你好";
		System.out.println("66666666666"+words);
		mTts.startSpeaking(words, mSynListener);

	}

	private static SynthesizerListener mSynListener = new SynthesizerListener() {

		public void onSpeakBegin() {
		}

		public void onBufferProgress(int progress, int beginPos, int endPos, String info) {

		}

		public void onSpeakPaused() {

		}

		public void onSpeakResumed() {

		}

		public void onSpeakProgress(int progress, int beginPos, int endPos) {

//			updateText(mText.substring(beginPos, endPos + 1));
		}

		public void onCompleted(SpeechError error) {

			String text = mText;
			if (null != error) {
				text = error.getErrorDescription(true);
			}

//			updateText(text);
		}

		public void onEvent(int eventType, int arg1, int arg2, int arg3, Object obj1, Object obj2) {
			if (SpeechEvent.EVENT_TTS_BUFFER == eventType) {
				ArrayList<?> bufs = null;
				if (obj1 instanceof ArrayList<?>) {
					bufs = (ArrayList<?>) obj1;
				} else {
				} // end of if-else instance of ArrayList

				if (null != bufs) {
					for (final Object obj : bufs) {
						if (obj instanceof byte[]) {
							final byte[] buf = (byte[]) obj;
						} else {
						}
					}
				}
			}
		}
	};

	// 初始化发音人镜像表，云端对应的本地
	// 为了查找本地资源方便，请把资源文件置为发音人参数+.jet，如“xiaoyan.jet”
	static void initVoiceMap() {
		mVoiceMap.clear();
		String[] names = null;

//		names = new String[2];
//		names[0] = names[1] = "xiaoyan";
//		mVoiceMap.put("小燕", names); // 小燕
//
//		names = new String[2];
//		names[0] = names[1] = "xiaoyu";
//		mVoiceMap.put("小宇", names); // 小宇
//
//		names = new String[2];
//		names[0] = "vixf";
//		names[1] = "xiaofeng";
//		mVoiceMap.put("小峰", names); // 小峰
//
//		names = new String[2];
//		names[0] = "vixm";
//		names[1] = "xiaomei";
//		mVoiceMap.put("小梅", names); // 小梅

		names = new String[2];
		names[0] = "vixr";
		names[1] = "xiaorong";
		mVoiceMap.put("小蓉", names); // 小蓉

		names = new String[2];
		names[0] = names[1] = "catherine";
		mVoiceMap.put("凯瑟琳", names); // 凯瑟琳
	}

	void setting() {
		final String engType = mParamMap.get(SpeechConstant.ENGINE_TYPE);
		String voiceName = null;

		for (Entry<String, String> entry : mParamMap.entrySet()) {
			String value = entry.getValue();
			if (SpeechConstant.VOICE_NAME.equals(entry.getKey())) {
				String[] names = mVoiceMap.get(entry.getValue());
				voiceName = value = SpeechConstant.TYPE_CLOUD.equals(engType) ? names[0] : names[1];
			}

			mTts.setParameter(entry.getKey(), value);
		}

		// 本地合成时设置资源，并启动引擎
		if (SpeechConstant.TYPE_LOCAL.equals(engType)) {
			// 启动合成引擎
			mTts.setParameter(ResourceUtil.ENGINE_START, SpeechConstant.ENG_TTS);
			// 设置资源路径
			String curPath = System.getProperty("user.dir");
			String resPath = ResourceUtil.generateResourcePath(curPath + "/tts/common.jet") + ";"
					+ ResourceUtil.generateResourcePath(curPath + "/tts/" + voiceName + ".jet");
			System.out.println("resPath=" + resPath);
			mTts.setParameter(ResourceUtil.TTS_RES_PATH, resPath);
		} // end of if is TYPE_LOCAL

		// 启用合成音频流事件，不需要时，不用设置此参数
		mTts.setParameter(SpeechConstant.TTS_BUFFER_EVENT, "1");
	}// end of function setting

	private static void initParamMap() {
		mParamMap.put(SpeechConstant.ENGINE_TYPE, DefaultValue.ENG_TYPE);
//		mParamMap.put(SpeechConstant.VOICE_NAME, DefaultValue.VOICE);//青年女声
		mParamMap.put(SpeechConstant.VOICE_NAME, "小蓉");
		mParamMap.put(SpeechConstant.BACKGROUND_SOUND, DefaultValue.BG_SOUND);
		mParamMap.put(SpeechConstant.SPEED, DefaultValue.SPEED);
		mParamMap.put(SpeechConstant.PITCH, DefaultValue.PITCH);
		mParamMap.put(SpeechConstant.VOLUME, DefaultValue.VOLUME);
		mParamMap.put(SpeechConstant.TTS_AUDIO_PATH, null);
	}

	private static void initMenu() {
		// 显示日志
		{
			Map<String, String> logMap = new LinkedHashMap<String, String>();
			logMap.put(Boolean.toString(true), "打开");
			logMap.put(Boolean.toString(false), "关闭");

		} // end of menuEng

		// 引擎类型选择
		{
			Map<String, String> engMap = new LinkedHashMap<String, String>();
			engMap.put(SpeechConstant.TYPE_CLOUD, "云端");
			engMap.put(SpeechConstant.TYPE_LOCAL, "本地");
		} // end of menuEng

		// 发音人选择
		{
			initVoiceMap();
			Map<String, String> voiceItemMap = new LinkedHashMap<String, String>();
			for (Entry<String, String[]> entry : mVoiceMap.entrySet()) {
				voiceItemMap.put(entry.getKey(), entry.getKey());
			}
		} 

		// 背景音乐
		{
			Map<String, String> bgMap = new LinkedHashMap<String, String>();
			bgMap.put("1", "开");
			bgMap.put("0", "关");
		} // end of menuBackGround

		// 语速、语调、音量
		{
			Map<String, String> percentMap = new LinkedHashMap<String, String>();
			percentMap.put(SpeechConstant.SPEED, "语速");
			percentMap.put(SpeechConstant.PITCH, "语调");
			percentMap.put(SpeechConstant.VOLUME, "音量");

		} // end of 语速，语调，音量

	}// end of function initMenu

}
